package com.source.barcodeMachineGrantAndRecovery.mapper;

import com.source.barcodeMachineGrantAndRecovery.VO.HouseMachineGrantVO;
import com.source.barcodeMachineGrantAndRecovery.entity.HouseMachineGrant;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author IceBerg777
 * @since 2021-04-14
 */
@Repository
public interface HouseMachineGrantMapper extends BaseMapper<HouseMachineGrant> {

    String SELECT = "SELECT hmg.*, sh.slaughterHouseName houseName, su.`name` userName ,gp.`name` grantPersonName , rp.`name` receivePersonName\n" +
            "FROM `house_machine_grant` hmg , slaughter_house sh, sys_user su , sys_user gp, sys_user rp\n" +
            "WHERE hmg.houseId = sh.slaughterHouseId AND sh.userId = su.userId AND hmg.grantPersonId = gp.userId AND hmg.receivePersonId = rp.userId AND hmg.isDelete = 0 ";

    String DISTINCT = SELECT + " AND hmg.id \n" +
            "IN (SELECT houseGrantId FROM barcode_machine) ";



    @Select(SELECT + " AND hmg.id = #{id}")
    HouseMachineGrantVO findById(String id);

    @Select(SELECT)
    List<HouseMachineGrantVO> findAll();

    @Select(SELECT + " AND concat_ws(',', sh.slaughterHouseName, machineNumber, grantDate, rent, expiringDate) like #{s}")
    List<HouseMachineGrantVO> findByLike(String s);

    @Select(DISTINCT + " AND hmg.machineNumber = #{machineNumber} ")
    HouseMachineGrant findByMachineNumber(String machineNumber);

    @Select("select * from `house_machine_grant` hmg where hmg.machineNumber = #{machineNumber} AND hmg.createDate Like #{date} ")
    HouseMachineGrant findByMachineNumberAndGrantDate(String machineNumber, String date);

    @Select(DISTINCT)
    List<HouseMachineGrantVO> findAllDistinct();

    @Select(DISTINCT + " AND concat_ws(',', sh.slaughterHouseName, machineNumber, grantDate, rent, expiringDate) like #{keyWord}")
    List<HouseMachineGrantVO> findDistinctByLike(String keyWord);

    @Select("SELECT * FROM `house_machine_grant` hmg where hmg.stallsGrantId IS NULL AND hmg.isDelete = 0 ")
    List<HouseMachineGrant> findUnGranted();
}
